import router from '@/router';
import { defineStore } from 'pinia';
import API from '../api';

interface userinfoState {
  userinfo: any;
  moneyinfo:any;
  webconfig: any;
  langList: any[];
  lang: any,
  raceRecent: any[];
};

const userinfo = defineStore({
    id: 'user',
    state: (): userinfoState => {
      return {
        userinfo: {},
        moneyinfo: {},
        webconfig: {},
        langList: [],
        lang: {
        },
        raceRecent: [],
      };
    },
    persist: true,
    getters: {
      getUserinfo: (state) => state.userinfo,
      getHaslogged: (state) => state.userinfo?.token,
      getUserMoney: (state) => state.moneyinfo?.money,
      getWebConfig: (state) => state.webconfig,
      getLangList: (state) => state.langList,
      getLang: (state) => state.lang,
      getRaceRecent: (state) => state.raceRecent,
    },
    actions: {
      setUserinfo(userinfo: any) {
        this.userinfo = userinfo;
      },
      // 退出登录
      logout(){
        this.userinfo = {};
        this.raceRecent = [];
        router.push({path:'/login'})
      },
      queryUserMoney() {
        return API.getMoneyAPI({}).then((res) => {
          if(res?.code === 200) {
            this.moneyinfo = res?.data;
          }
        });
      },
      queryWebConfig() {
        API.webConfigAPI({}).then((res) => {
          if(res?.code === 200) {
            this.webconfig = res.data;
          }
        })
      },
      setActiveLang(lang: any) {
        this.lang = lang;
      },
      setLangList(list: any[]) {
        this.langList = list;
      },
      setRaceRecent(list: any[]) {
        this.raceRecent = list;
      }
    },
  });
  
  export default userinfo;
